package com.xxxx.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.server.pojo.Menu;
import com.xxxx.server.pojo.MenuRole;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IMenuRoleService;
import com.xxxx.server.service.IMenuService;
import com.xxxx.server.service.IRoleService;
import com.xxxx.server.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 权限组
 * @author: Wy
 * @create: 2021-11-23 15:23
 **/
@RestController
@RequestMapping("/system/basic/permiss")
@Api(tags = "权限组管理")
public class PermissionController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuRoleService menuRoleService;

    @GetMapping("/")
    @ApiOperation(value = "获取所有角色")
    public RespBean<List<Role>> getAllRole(){
        List<Role> list = roleService.list();
        return new RespBean<List<Role>>().success("ok",list);
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("/")
    public RespBean addRole(@RequestBody Role role){
        if (!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        if (roleService.save(role)){
            return new RespBean().success("ok");
        }
        return new RespBean().error("error");
    }

    @ApiOperation("修改角色")
    @PutMapping("/")
    public RespBean editRole(@RequestBody Role role){
        if (!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        if (roleService.updateById(role)){
            return new RespBean().success("ok");
        }
        return new RespBean().error("error");
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/role/{rid}")
    public RespBean delRole(@PathVariable Integer rid){
        if (roleService.removeById(rid)){
            return new RespBean().success("ok");
        }
        return new RespBean().error("error");
    }

    @ApiOperation("查询所有菜单")
    @GetMapping("/menus")
    public RespBean<List<Menu>> getAllMenus(){
        List<Menu> menuList = menuService.getAllMenus();
        return new RespBean<List<Menu>>().success("ok",menuList);
    }

    @ApiOperation("根据角色ID查询菜单Id")
    @GetMapping("/mid/{rid}")
    public RespBean<List<Integer>> getMinByRid(@PathVariable Integer rid){
        List<Integer> midList = menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid", rid))
                .stream()
                .map(MenuRole::getMid)
                .collect(Collectors.toList());
        return new RespBean<List<Integer>>().success("ok",midList);
    }

    @ApiOperation("更新角色菜单")
    @PutMapping("/roleMenu")
    public RespBean updateMenuRole(Integer rid,Integer[] mids){
       return menuRoleService.updateMenuRole(rid,mids);
    }
}
